﻿








#region Designer generated code
using System;

using TrueCode.Core;
using TrueCode.Core.EditableEntity;
using EMan.Common.EnterpriseStructure;

namespace EMan.Interface.EnterpriseStructure.Employees
{
	
	public partial class EmployeeEditable : IEditableEntity< Employee >, IDisposable
	{
		private readonly EmployeeEditable _sessionParent;
		private readonly Employee _entity;
		private EditableObjectState _state;
		
				
		#region Original values
		private EditableObjectState _stateBeforeDeleted;

						
			private System.String _NickName;
									
			private System.String _Firstname;
									
			private System.String _Secondname;
									
			private System.String _Surname;
														
			private EMan.Common.EnterpriseStructure.Job _Job;
							#endregion
	
		#region .ctors
		public EmployeeEditable( Employee entity, bool isNew = false )
		{
			if( entity == null ) throw new ArgumentNullException( "entity" );
			
			_entity = entity;
			if( !isNew )
				StoreOriginalValues();
			_state = isNew ? EditableObjectState.New : EditableObjectState.Original;
		}
		
		private EmployeeEditable( Employee entity, EmployeeEditable sessionParent ) : this( entity )
		{
		  _sessionParent = sessionParent;
		}
		#endregion
	
		#region Entity Properties
					
			public System.String NickName
			{
				get{ return _entity.NickName; }
				
								set
				{
					_entity.NickName = value;
					SetAsModified();
					PropertyChanged.Raise( this, () => NickName );
				}
							}
			
					
			public System.String Firstname
			{
				get{ return _entity.Firstname; }
				
								set
				{
					_entity.Firstname = value;
					SetAsModified();
					PropertyChanged.Raise( this, () => Firstname );
				}
							}
			
					
			public System.String Secondname
			{
				get{ return _entity.Secondname; }
				
								set
				{
					_entity.Secondname = value;
					SetAsModified();
					PropertyChanged.Raise( this, () => Secondname );
				}
							}
			
					
			public System.String Surname
			{
				get{ return _entity.Surname; }
				
								set
				{
					_entity.Surname = value;
					SetAsModified();
					PropertyChanged.Raise( this, () => Surname );
				}
							}
			
					
			public EMan.Common.EnterpriseStructure.Department Department
			{
				get{ return _entity.Department; }
				
							}
			
					
			public EMan.Common.EnterpriseStructure.Job Job
			{
				get{ return _entity.Job; }
				
								set
				{
					_entity.Job = value;
					SetAsModified();
					PropertyChanged.Raise( this, () => Job );
				}
							}
			
				#endregion

		#region Implementation of IEditableEntity
		public Employee Entity
		{
			get{ return _entity; }
		}
		
		public EditableObjectState State
		{
			get { return _state; }
			private set
			{
				if( _state != value )
				{
					_state = value;
					PropertyChanged.Raise( this, () => State );
				}
			}
		}
		
		public void AcceptChanges()
		{
			if( _state == EditableObjectState.Deleted )
				throw new InvalidOperationException( "Can not accept changes of the deleted entity " );
				
			if( _state != EditableObjectState.Original )
			{
				StoreOriginalValues();
				State = EditableObjectState.Original;
				
				if( _sessionParent != null )
					_sessionParent.EditSessionCommitted();
			}
		}
		
		public void RejectChanges()
		{
			if( _state == EditableObjectState.New )
				throw new InvalidOperationException( "Can not reject changes of the New entity " );

			if( State == EditableObjectState.Deleted )
				State = _stateBeforeDeleted;
			else
			{
														_entity.NickName = _NickName;
																			_entity.Firstname = _Firstname;
																			_entity.Secondname = _Secondname;
																			_entity.Surname = _Surname;
																												_entity.Job = _Job;
													
				PropertyChanged.Raise( this, null );
				State = EditableObjectState.Original;
			}
		}
		
		public void MarkDeleted()
		{
			if( _state == EditableObjectState.New )
				throw new InvalidOperationException( "Can not mark New entity as Deleted" );
			// If RejectChanges will be called, we should reject MarkDeleted change
			_stateBeforeDeleted = State;
			State = EditableObjectState.Deleted;

			OnMarkedDeleted();
		}
		
		public void UpdateAll( Employee entity )
		{
											
				_entity.Firstname = entity.Firstname;
				
															
				_entity.Secondname = entity.Secondname;
				
															
				_entity.Surname = entity.Surname;
				
															
				_entity.Id = entity.Id;
				
															
				_entity.Revision = entity.Revision;
				
										
			SetAsModified();
			PropertyChanged.Raise( this, null );
		}
		
		#endregion
		
		#region Entry points
		public EmployeeEditable StartNewEditSession()
		{
			return new EmployeeEditable( _entity, this );
		}
		#endregion
		
		#region Protected
		protected void EditSessionCommitted()
		{
			SetAsModified();
			PropertyChanged.Raise( this, null );
		}
		
		protected virtual void OnMarkedDeleted(){}
		#endregion

    	#region Private	
		private void SetAsModified()
		{
			if( State == EditableObjectState.Deleted )
				throw new InvalidOperationException( "Can not modify deleted entity, use RejectChanges to undelete it" );

			if( State != EditableObjectState.New && State != EditableObjectState.Modified )
				State = EditableObjectState.Modified;
		}
		
		private void StoreOriginalValues()
		{
											_NickName = _entity.NickName;
															_Firstname = _entity.Firstname;
															_Secondname = _entity.Secondname;
															_Surname = _entity.Surname;
																						_Job = _entity.Job;
									}
		#endregion
	
		#region IDisposable implementation
		public void Dispose()
		{
			if( _sessionParent != null )
			{
				if( _state == EditableObjectState.Modified ) // it is a not commited edit session 
					RejectChanges();
			}
		}
		#endregion
		
		#region Implementation of INotifyPropertyChanged
		public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

		protected System.ComponentModel.PropertyChangedEventHandler PropertyChangedEvent
		{
		get{ return PropertyChanged; }
		}
		#endregion
	}
	
}

#endregion